package com.sh.data.engine.application.service.normalization;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.normalization.standard.*;
import com.sh.data.engine.application.model.mapper.normalization.DictionaryMappingDetailResponsePageMapper;
import com.sh.data.engine.application.model.mapper.normalization.DictionaryMappingResponsePageMapper;
import com.sh.data.engine.application.model.mapper.normalization.MappingReportResponsePageMapper;
import com.sh.data.engine.application.model.request.normalization.*;
import com.sh.data.engine.common.exception.InvalidParamException;
import com.sh.data.engine.common.util.excel.EasyExcelUtil;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.normalization.standard.job.DictionaryMappingTask;
import com.sh.data.engine.domain.normalization.standard.model.domain.GetDictionaryDomain;
import com.sh.data.engine.domain.normalization.standard.model.domain.MappingReportDomain;
import com.sh.data.engine.domain.normalization.standard.model.param.*;
import com.sh.data.engine.domain.normalization.standard.service.IDictionaryMappingDetailService;
import com.sh.data.engine.domain.normalization.standard.service.IDictionaryMappingReportService;
import com.sh.data.engine.domain.normalization.standard.service.IDictionaryMappingService;
import com.sh.data.engine.domain.util.ConvertUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2024/7/3 14:28
 */
@Slf4j
@Service
public class DictionaryMappingApplicationService {

    @Autowired
    private IDictionaryMappingService dictionaryMappingService;

    @Autowired
    private IDictionaryMappingDetailService dictionaryMappingDetailService;

    @Autowired
    private IDictionaryMappingReportService dictionaryMappingReportService;

    @Resource
    private DictionaryMappingTask dictionaryMappingTask;

    @Autowired
    private DictionaryMappingResponsePageMapper dictionaryMappingResponsePageMapper;

    @Autowired
    private DictionaryMappingDetailResponsePageMapper dictionaryMappingDetailResponsePageMapper;

    @Autowired
    private MappingReportResponsePageMapper mappingReportResponsePageMapper;

    public PagerResponse<DictionaryMappingResponse> listDictionaryMapping(DictionaryQueryRequest request) {
        request.setReleaseStatus(1);
        PageResult<DictionaryMappingResponse> pageResult = ConvertUtil.convertPageInfo(
            dictionaryMappingService.listDictionaryMapping(
                ConvertUtil.copyProperties(request, DictionaryQueryParam.class)),
            DictionaryMappingResponse.class);
        return dictionaryMappingResponsePageMapper.toPageResponse(pageResult);
    }

    public PagerResponse<DictionaryMappingDetailResponse> detailListDictionaryMapping(DictionaryMappingDetailQueryRequest request) {
        DictionaryMappingDetailQueryParam param =
            ConvertUtil.copyProperties(request, DictionaryMappingDetailQueryParam.class);
        param.setMapSource(0);
        PageResult<DictionaryMappingDetailResponse> pageResult = ConvertUtil.convertPageInfo(
            dictionaryMappingService.detailListDictionaryMapping(param),
            DictionaryMappingDetailResponse.class);
        return dictionaryMappingDetailResponsePageMapper.toPageResponse(pageResult);
    }

    public boolean detailBound(MappingDetailRequest request) {
        if (request.getBoundStatus() == null
            || request.getBoundStatus() < 0
            || request.getBoundStatus() > 1) {
            throw new InvalidParamException("绑定状态参数不合法");
        }
        dictionaryMappingService.detailBound(
            ConvertUtil.copyProperties(request, MappingDetailParam.class));
        return true;
    }

    public boolean detailMaintain(MappingDetailRequest request) {
        dictionaryMappingDetailService.detailMaintain(
            ConvertUtil.copyProperties(request, MappingDetailParam.class));
        return true;
    }

    public MappingReportInfoResponse reportInfo() {
        return ConvertUtil.copyProperties(
            dictionaryMappingService.reportInfo(), MappingReportInfoResponse.class);
    }

    public List<GetDictionaryDetailResponse> getDictionaryDetail(String dictNameEn, String dictNameCn) {
        List<GetDictionaryDomain> dictionaryList =
            dictionaryMappingService.getDictionaryList(dictNameEn, dictNameCn);
        return ConvertUtil.copyProperties(dictionaryList, GetDictionaryDetailResponse.class);
    }

    public PagerResponse<MappingReportResponse> listReport(MappingReportDetailRequest request) {
        PageResult<MappingReportResponse> pageResult = ConvertUtil.convertPageInfo(
            dictionaryMappingReportService.listReport(
                ConvertUtil.copyProperties(request, MappingReportDetailParam.class), 0),
            MappingReportResponse.class);
        return mappingReportResponsePageMapper.toPageResponse(pageResult);
    }

    public List<DictionaryMappingReportDetailResponse> listReportDetail(MappingReportRequest request) {
        return ConvertUtil.copyProperties(
            dictionaryMappingReportService.reportDetail(
                ConvertUtil.copyProperties(request, MappingReportParam.class)),
            DictionaryMappingReportDetailResponse.class);
    }

    public List<TableStandardResponse> tableStandardList(String tableName) {
        if (StringUtils.isBlank(tableName)) {
            throw new InvalidParamException("表名为必传字段");
        }
        return ConvertUtil.copyProperties(
            dictionaryMappingReportService.tableStandardList(tableName),
            TableStandardResponse.class);
    }

    public void listReportExport(HttpServletResponse response, MappingReportDetailRequest request) {
        PageResult<MappingReportDomain> pageInfo =
            dictionaryMappingReportService.listReport(
                ConvertUtil.copyProperties(request, MappingReportDetailParam.class), 1);
        String[] columnNames = {"表名称", "表描述", "命中数量/字段数量", "命中比例"};
        List<List<String>> dataList = new ArrayList<>(pageInfo.getPageSize());
        dataList.add(Arrays.asList(columnNames));
        pageInfo
            .getResult()
            .forEach(
                report -> {
                    List<String> item = new ArrayList<>();
                    item.add(report.getTblName());
                    item.add(report.getTblDes());
                    item.add(report.getHitStr());
                    item.add(report.getPercentStr());
                    dataList.add(item);
                });
        EasyExcelUtil.exportExcel(response, "映射报告", dataList);
    }

    public String getLastTaskExecuteTimeStamp() {
        return dictionaryMappingTask.getLastTaskExecuteTimeStamp();
    }
}
